define(["jquery", "template"], ($, template) => {
    class HeaderRepeat{
        constructor() {
            this.urlStr = null;
            this.userTel = "";
            this.init()
        }
        init() {
            this.putData()
            .then(() => {
                this.pageInit();
            })
        }

        // 放置页面头部html结构
        putData() {
            return new Promise((resolve) => {
                // 导入header的html结构
                $(".headerBox").load("/html/modules/headerRepeat.html", () => {
                    resolve();
                });
            })
        }
        // 页面设置
        pageInit() {
            const _this = this;

            // 验证登录情况
            if (localStorage.getItem("currentUser")) {
                _this.userTel = localStorage.getItem("currentUser");
            } else {
                _this.userTel = "游客";
                localStorage.setItem("currentUser", "游客")
            }
            // console.log(_this.userName)
            $("#header .userBox .header p")
            .text(_this.userTel);

            // 用户栏出现事件
            $("#header .right .userLogo")
            .mouseenter(function () {
                $(this).nextAll(".userBox").addClass("active");
                $("#header .right .cartBox").removeClass("active");
            })
            .mouseleave(function () {
                _this.eventId = setTimeout(() => {
                    // console.log(this)
                    $(this).nextAll(".userBox").removeClass("active");
                }, 1000);
            })
            $("#header .right .userBox")
            .mouseenter(function () {
                clearTimeout(_this.eventId);
            })
            .mouseleave(function () {
                $(this).removeClass("active");
            })
            
            // 用户栏点击事件
            $("#header .right .userLogo")
            .click(function () {
                if (_this.userTel === "游客") {
                    window.open("/html/login.html", "_self");
                }
            })

            // 退出登录事件
            $("#header .userBox .list")
            .last()
            .click(function () {
                _this.userTel = "游客";
                localStorage.setItem("currentUser", _this.userTel);
                location.reload();
            })

            



            _this.urlStr = location.href;
            // 非首页，网页logo可点击
            if (_this.urlStr.indexOf("index") === -1) {
                $("#header h1 i")
                .css("cursor", "pointer")
                .click(function () {
                    window.open("/index.html", "_self");
                })
            }
            // 在购物车页，购物车logo消失
            if (_this.urlStr.indexOf("cart") !== -1) {
                $("#header .right .cartLogo")
                .css("display", "none");
            }

            // 购物车是否有货，出现小圆点
            let hasCart = false;
            let hasUser = false;
            let hasGoods = false;
            let userIndex = null;
            let goodsIndex = null;
            this.cartData = JSON.parse(localStorage.getItem("cartData"));
            hasCart = this.cartData && this.cartData.cartList ? true : false;
            if (hasCart) {
                this.cartData.cartList.forEach((item, index) => {
                    if (item.userTel === this.userTel) {
                        hasUser = true;
                        userIndex = index;
                    }
                })
            }
            if (hasUser) {
                hasGoods = this.cartData.cartList[userIndex].goodsList.length ? true : false;
            }
            // console.log(hasCart, hasUser, hasGoods);

            // 有商品信息时
            if (hasGoods) {
                $("#header .cartBox .cart").css("display", "block");
                $("#header .cartBox .emptyBox").css("display", "none");
                this.goodsList = this.cartData.cartList[userIndex].goodsList;
                // console.log(this.goodsList);
                const htmlStr = template("cartTemplate", {data : this.goodsList});
                $("#cartTemplateBox").html(htmlStr);
                this.allCountInit();
                this.allPriceInit();
                $("#header .cartLogo .point").addClass("active");
            } else{
                $("#header .cartBox .cart").css("display", "none");
                $("#header .cartBox .emptyBox").css("display", "flex");
                $("#header .cartLogo .point").removeClass("active");
            }

            
            
            // 购物车表出现事件
            $("#header .right .cartLogo")
            .mouseenter(function () {
                $(this).nextAll(".cartBox").addClass("active");
                $("#header .right .userBox").removeClass("active");
            })
            .mouseleave(function () {
                _this.eventId = setTimeout(() => {
                    // console.log(this)
                    $(this).nextAll(".cartBox").removeClass("active");
                }, 1000);
            })
            $("#header .right .cartBox")
            .mouseenter(function () {
                clearTimeout(_this.eventId);
            })
            .mouseleave(function () {
                $(this).removeClass("active");
            })

            // 购物车logo点击事件
            $("#header .right .cartLogo .cart")
            .click(function () {
                window.open("/html/cart.html", "_self");
            })

            // 购物车删除按钮点击事件
            $("#header .cartList .delBtn")
            .click(function () {
                goodsIndex = $(this).parents(".cartList").index();
                // console.log(goodsIndex);
                $("#header .cartBox .cartList").eq(goodsIndex).remove();
                _this.goodsList.splice(goodsIndex, 1);
                _this.cartData.cartList[userIndex].goodsList = _this.goodsList;
                localStorage.setItem("cartData", JSON.stringify(_this.cartData));
                // console.log(_this.goodsList);
                if (_this.goodsList.length >0) {
                    _this.allCountInit();
                    _this.allPriceInit();
                } else {
                    $("#header .cartBox .cart").css("display", "none");
                    $("#header .cartBox .emptyBox").css("display", "flex");
                    $("#header .cartLogo .point").removeClass("active");
                }
            })


        }
        // 总数量初始化
        allCountInit() {
            let count = 0;
            this.goodsList.forEach((item) => {
                count += item.count;
            })
            $("#header .cartFooter .allCount").text(count);
        }
        // 总价格初始化
        allPriceInit() {
            let price = 0;
            this.goodsList.forEach((item) => {
                price += item.count * item.price;
            })
            $("#header .cartFooter .allPrice").text(price);
        }
    }
    return HeaderRepeat;
})
